package com.microinvest.service.impl;
import com.microinvest.dao.AdminMapper;
import com.microinvest.entity.Users;
import com.microinvest.entity.BuyStock;
import com.microinvest.entity.SellStock;
import com.microinvest.service.AdminService;
import com.microinvest.util.Result;
import com.microinvest.util.SHA256Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    private final AdminMapper adminMapper;

    @Autowired
    public AdminServiceImpl(AdminMapper adminMapper){
        this.adminMapper=adminMapper;
    }


    @Override
    public Result<String> loginWithAdminNamePasswordService(String adminName, String password) {
        // 检查用户是否存在
        boolean AdminExists = adminMapper.existsByAdminName(adminName);
        if (!AdminExists) {
            return Result.error("用户不存在");
        }
        // 比对加密后的密码的哈希值
        String encryptedPassword = SHA256Util.getSHA256String(password);

        boolean passwordCorrect = adminMapper.comparePassword(adminName, encryptedPassword);
        if (!passwordCorrect) {
            return Result.error("用户名或密码错误");
        }

        return Result.success();
    }

    @Override
    public Result<List<Users>> getUsersMessageService(){
        //返回用户所有信息（列表形式）
        return Result.success(adminMapper.getUsersMessage());

    }

    @Override
    public Result<List<BuyStock>> getBuyStockMessage(){
        //返回所有股票买入信息（列表形式）
        return Result.success(adminMapper.getBuyStockMessage());

    }

    @Override
    public Result<List<SellStock>> getSellStockMessage(){
        //返回所有股票卖出信息（列表形式）
        return Result.success(adminMapper.getSellStockMessage());

    }
}
